package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DocProfileEntity;

public class DocProfileDAO {
	public DocProfileEntity findByUserId(Connection conn,int user_id) throws SQLException {
		DocProfileEntity entity=null;
		PreparedStatement ps=conn.prepareStatement("select * from doc_profile where user_id=? and status=1");
		ps.setInt(1, user_id);
		ResultSet rs=ps.executeQuery();
		if(rs.next()) {
			entity=fill(rs);
		}
		return entity;
	}

	private DocProfileEntity fill(ResultSet rs) throws SQLException {
		DocProfileEntity entity=new DocProfileEntity();
		entity.setId(rs.getInt("id"));
		entity.setInst_id(rs.getInt("inst_id"));
		entity.setName(rs.getString("name"));
		entity.setTitle(rs.getString("title"));
		entity.setUser_id(rs.getInt("user_id"));
		entity.setPhone(rs.getString("phone"));
		entity.setMemo(rs.getString("memo"));
		entity.setStatus(rs.getInt("status"));
		entity.setAddress(rs.getString("address"));
		entity.setGood(rs.getString("good"));
		entity.setNickname(rs.getString("nickname"));
		entity.setIdcard(rs.getString("idcard"));
		entity.setGender(rs.getInt("gender"));
		entity.setRule_level(rs.getInt("rule_level"));
		return entity;
	}

	public boolean insert(Connection conn, DocProfileEntity docProfileEntity) throws SQLException {
		boolean b=false;
		PreparedStatement ps=conn.prepareStatement("insert into doc_profile(inst_id,name,title,user_id,phone,memo,status,good,idcard,nickname,address,gender,rule_level) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
		ps.setInt(1, docProfileEntity.getInst_id());
		ps.setString(2, docProfileEntity.getName());
		ps.setString(3, docProfileEntity.getTitle());
		ps.setInt(4, docProfileEntity.getUser_id());
		ps.setString(5, docProfileEntity.getPhone());
		ps.setString(6, docProfileEntity.getMemo());
		ps.setInt(7, docProfileEntity.getStatus());
		ps.setString(8, docProfileEntity.getGood());
		ps.setString(9, docProfileEntity.getIdcard());
		ps.setString(10, docProfileEntity.getNickname());
		ps.setString(11, docProfileEntity.getAddress());
		ps.setInt(12, docProfileEntity.getGender());
		ps.setInt(13, docProfileEntity.getRule_level());
		if(ps.executeUpdate()>0) {
			b=true;
		}
		return b;
	}
	public boolean update(Connection conn, DocProfileEntity docProfileEntity) throws SQLException {
		boolean b=false;
		PreparedStatement ps=conn.prepareStatement("update doc_profile set inst_id=?,name=?,title=?,user_id=?,phone=?,memo=?,status=?,good=?,idcard=?,nickname=?,address=?,gender=?,rule_level=? where id=?");
		ps.setInt(1, docProfileEntity.getInst_id());
		ps.setString(2, docProfileEntity.getName());
		ps.setString(3, docProfileEntity.getTitle());
		ps.setInt(4, docProfileEntity.getUser_id());
		ps.setString(5, docProfileEntity.getPhone());
		ps.setString(6, docProfileEntity.getMemo());
		ps.setInt(7, docProfileEntity.getStatus());
		ps.setString(8, docProfileEntity.getGood());
		ps.setString(9, docProfileEntity.getIdcard());
		ps.setString(10, docProfileEntity.getNickname());
		ps.setString(11, docProfileEntity.getAddress());
		ps.setInt(12, docProfileEntity.getGender());
		ps.setInt(13, docProfileEntity.getRule_level());
		ps.setInt(14, docProfileEntity.getId());
		if(ps.executeUpdate()>0) {
			b=true;
		}
		return b;
	}

	public List<DocProfileEntity> listDirectorManaged(Connection conn,int inst_id, int rule_level) throws SQLException {
		List<DocProfileEntity> list=new LinkedList<DocProfileEntity>();
		PreparedStatement ps=conn.prepareStatement("select * from doc_profile where inst_id=? and rule_level>=? and status=1");
		ps.setInt(1, inst_id);
		ps.setInt(2, rule_level);
		ResultSet rSet=ps.executeQuery();
		while (rSet.next()) {
			list.add(fill(rSet));
		}
		return list;
	}
	
	public List<DocProfileEntity> listManaged(Connection conn,int inst_id, int rule_level) throws SQLException {
		List<DocProfileEntity> list=new LinkedList<DocProfileEntity>();
		PreparedStatement ps=conn.prepareStatement("select * from doc_profile where inst_id=? and rule_level>? and status=1");
		ps.setInt(1, inst_id);
		ps.setInt(2, rule_level);
		ResultSet rSet=ps.executeQuery();
		while (rSet.next()) {
			list.add(fill(rSet));
		}
		return list;
	}
}
